package com.woniu.user.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.dto.ResourceDTO;
import com.woniu.form.InsertPermissionForm;
import com.woniu.param.InsertPermissionParam;
import com.woniu.user.service.PermissionService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author ly
 * @since 2024年12月05日
 */
@RestController
@RequestMapping("/permission")
@Api(tags = "资源管理")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    @PostMapping("/add")
    @ApiOperation("添加资源")
    public Result add(@RequestBody InsertPermissionForm ipf) throws Exception{
        permissionService.addPermission(BeanUtil.toBean(ipf, InsertPermissionParam.class));
        return Result.getResult(String.class).setCode(200).setMsg("添加成功");
    }

    /**
     * 查询管理员的菜单资源
     */
    @GetMapping("/selectAdminResource")
    @ApiOperation("查询管理员菜单资源接口")
    public Result selectAdminResource(@RequestHeader("token") String token)throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String adminId = claims.get("adminId").toString();
        List<ResourceDTO> resource = permissionService.selectAdminResource(adminId);
        return Result.getResult(List.class).setCode(200).setMsg("查询成功").setData(resource);
    }

    /**
     * 查询管理员的按钮资源
     */
    @GetMapping("/selectAdminButton")
    @ApiOperation("查询管理员按钮资源接口")
    public Result selectAdminButton(@RequestHeader("token") String token,Integer menuId)throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String adminId = claims.get("adminId").toString();
        List<String> resource = permissionService.selectAdminButton(adminId,menuId);
        return Result.getResult(List.class).setCode(200).setMsg("查询成功").setData(resource);
    }

    /**
     * 查询管理员所有权限资源
     */
    @GetMapping("/selectAdminAll")
    @ApiOperation("查询所有权限资源接口")
    public Result selectAdminAll(@RequestHeader("token") String token)throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String adminId = claims.get("adminId").toString();
        permissionService.selectAdminAll(adminId);
        return Result.getResult(String.class).setCode(200).setMsg("查询成功");
    }

    /**
     * 查询用户的菜单资源
     */
    @GetMapping("/selectUserResource")
    @ApiOperation("查询管理员菜单资源接口")
    public Result selectUserResource(@RequestHeader("token") String token)throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        List<ResourceDTO> resource = permissionService.selectUserResource(userId);
        return Result.getResult(List.class).setCode(200).setMsg("查询成功").setData(resource);
    }

    /**
     * 查询用户的按钮资源
     */
    @GetMapping("/selectUserButton")
    @ApiOperation("查询管理员按钮资源接口")
    public Result selectUserButton(@RequestHeader("token") String token,Integer menuId)throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        List<String> resource = permissionService.selectUserButton(userId,menuId);
        return Result.getResult(List.class).setCode(200).setMsg("查询成功").setData(resource);
    }

    /**
     * 查询所有权限资源
     */
    @GetMapping("/selectUserAll")
    @ApiOperation("查询所有权限资源接口")
    public Result selectUserAll(@RequestHeader("token") String token)throws Exception{
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        permissionService.selectUserAll(userId);
        return Result.getResult(String.class).setCode(200).setMsg("查询成功");
    }
}

